package mp.study.demo.controller;

import mp.study.demo.bean.School;
import mp.study.demo.bean.Student;
import mp.study.demo.service.SchAndStuService;
import mp.study.demo.service.SchoolService;
import mp.study.demo.service.StudentService;
import mp.study.demo.vo.SchoolAndStudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/6/18 19:39
 */
@RestController
public class StuAndSchController
{
    @Autowired
    private SchAndStuService service;

    @Autowired
    private StudentService studentService;

    @Autowired
    private SchoolService schoolService;

    @GetMapping("/test/{id}")
    public List<SchoolAndStudentVO> getInfoById(@PathVariable("id") Integer id)
    {
        SchoolAndStudentVO allInfo = service.getInfoById(id);
        ArrayList<SchoolAndStudentVO> list = new ArrayList<>();
        list.add(allInfo);
        return list;
    }

    @GetMapping("/test")
    public List<SchoolAndStudentVO> getAllInfo()
    {
        return service.getAllInfo();
    }

    @PostMapping("/add")
//    @Transactional
    public void add(SchoolAndStudentVO schoolAndStudentVO)
    {
//        SchoolAndStudentVO schoolAndStudentVO = new SchoolAndStudentVO();
        School school = new School();
        school.setName(schoolAndStudentVO.getName());
        schoolService.saveOrUpdate(school);
        List<Student> students = schoolAndStudentVO.getStudents();
        studentService.saveOrUpdateBatch(students);
    }
}
